//
//  MapViewController.m
//  Crocodile
//
//  Created by CONG TRAN on 9/10/12.
//  Copyright (c) 2012 hust. All rights reserved.
//

#import "MapViewController.h"
#import <MapKit/MapKit.h>
#import "Annotation.h"
#import "Constant.h"


@interface MapViewController ()

@end

@implementation MapViewController
@synthesize annonation;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    UIImage *headerImg = [UIImage imageNamed:kImgHeader];
    UIImageView *headerImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    [headerImgView setImage:headerImg];
    [self.view addSubview:headerImgView];
    [headerImg release];
    [headerImgView release];
    
    // Title
    UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 3, 170, 34)];
    titleLbl.backgroundColor = [UIColor clearColor];
    titleLbl.center = CGPointMake(160, 20);
    titleLbl.textAlignment = UITextAlignmentCenter;
    titleLbl.textColor = [UIColor greenColor];
    titleLbl.font = [UIFont boldSystemFontOfSize:20];
    titleLbl.text = kStrShowTabTitle;
    [self.view addSubview:titleLbl];
    [titleLbl release];
    
    UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 60, 30)];
    [backButton setTitle:@"Back" forState:UIControlStateNormal];
    backButton.backgroundColor = [UIColor redColor];
    [backButton addTarget:self action:@selector(goBackToShows) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    [backButton release];
    
    mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 44, 320, 436)];
    mapView.mapType = MKMapTypeStandard;
    
    MKCoordinateRegion region;
	MKCoordinateSpan span;
	span.latitudeDelta=0.05;
	span.longitudeDelta=0.05;
	
//	CLLocationCoordinate2D location;
//    location.latitude = 21.033333;
//    location.longitude = 105.850000;
    
    
	region.span=span;
	region.center=annonation.coordinate;
	
	
    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    
    [point setCoordinate:annonation.coordinate];
    [point setTitle:annonation.titleAnnonation];
    [point setSubtitle:annonation.subTitle];
    
	[mapView addAnnotation:point];
    //[mapView selectAnnotation:point animated:YES];
	
	[mapView setRegion:region animated: YES];
	[mapView regionThatFits:region];
    [self.view addSubview:mapView];
    
//    CLLocationCoordinate2D coord = {.latitude =  21.033333, .longitude =  105.850000};
//    MKCoordinateSpan span = {.latitudeDelta =  0.006, .longitudeDelta =  0.006};
//    MKCoordinateRegion region = {coord, span};
//    
//    
//    CLLocationCoordinate2D location;
//	location.latitude = 21.033333;
//	location.longitude = 105.850000;
//	
//	
//	if(addAnnotation != nil) {
//		[mapView removeAnnotation:addAnnotation];
//		[addAnnotation release];
//		addAnnotation = nil;
//	}
//	
//	addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];
//	[mapView addAnnotation:addAnnotation];
//
//    
//    
//    [mapView setRegion:region animated:YES];
//    [self.view addSubview:mapView];

	// Do any additional setup after loading the view.
}

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
	MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
	annView.pinColor = MKPinAnnotationColorPurple;
	annView.animatesDrop=TRUE;
	annView.canShowCallout = YES;
	annView.calloutOffset = CGPointMake(-5, 5);
	return annView;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void) goBackToShows
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
